//
//  YYLNetTools.swift
//  06swift网络请求封装
//
//  Created by yangyinglei on 2017/3/28.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit
import AFNetworking

enum RequsetType: Int {
    case get = 0
    case post = 1
}
//  封装网络请求工具类
class YYLNetTools: AFHTTPSessionManager {

    static let sharedTools: YYLNetTools = {
        let tool = YYLNetTools()
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
    return tool
    }()
    //请求的方法
    fileprivate func request(request: RequsetType, urlStr: String, parameters: Any?, finishCallBack:@escaping (Any?, Error?)->Void) {
        if request == .get {
        get(urlStr, parameters: parameters, progress: nil, success: { (_, data) in
            finishCallBack(data,nil)
        }, failure: { (_, error) in
            finishCallBack(nil,error)
        })
        } else {
        post(urlStr, parameters: parameters, progress: nil, success: { (_, data) in
            finishCallBack(data,nil)
        }, failure: { (_, error) in
            finishCallBack(nil,error)
        })

        }
    }
}
//OAutn登录相关接口
extension YYLNetTools {

    //  通过code获取accessToken的方法
    func requestaccessToken(code: String, finishCallBack: @escaping (Any?, Error?) -> Void) {
        
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        let param: [String: Any] = [
            "client_id" : appkey,
            "client_secret" : client_secret,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : redirect_uri
        ]
        //发送网罗请求
        self.request(request: .post, urlStr: urlStr, parameters: param, finishCallBack: finishCallBack)
    }
    
    //  通过accessToken和uid获取用户信息的方法
    func requestUserInfo(access_token: String,uid: Int64, finishCallBack: @escaping (Any?, Error?) -> Void) {
        
        let urlStr = "https://api.weibo.com/2/users/show.json"
        //        print(urlStr + "?access_token=\(access_token)&uid=\(uid)")
        let param: [String: Any] = [
            "access_token" : access_token,
            "uid" : uid,
            ]
        //  发送网络请求
        self.request(request: .get, urlStr: urlStr, parameters: param, finishCallBack: finishCallBack)
    }

}

//获取主页信息 首页相关接口
extension YYLNetTools {
    //  获取当前登录用户及其所关注（授权）用户的最新微博
    func requestHomeData(access_token: String, max_id: Int64 = 0, since_id: Int64 = 0,finishCallBack: @escaping (Any?, Error?) -> Void){
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        //        print(urlStr + "?access_token=\(access_token)")
        let param: [String: Any] = [
            "access_token" : access_token,
            "max_id" : max_id,
            "since_id" : since_id
        ]
        //  发送网络请求
        self.request(request: .get, urlStr: urlStr, parameters: param, finishCallBack: finishCallBack)
        
    }
    //发送文字微博   https://api.weibo.com/2/statuses/update.json
    func sendWeiboMessage(access_token: String, status: String,finishCallBack: @escaping (Any?, Error?) -> Void) {
        let urlStr = "https://api.weibo.com/2/statuses/update.json"
        
        let param: [String: Any] = [
            "access_token" : access_token,
            "status" : status
        ]
    
        request(request: .post, urlStr: urlStr, parameters: param, finishCallBack: finishCallBack)

    }
    //发送文字和一张图片微博 https://upload.api.weibo.com/2/statuses/upload.json
    func sendWeiboMessageAndOnePic(access_token: String, status: String, pic: UIImage,finishCallBack: @escaping (Any?, Error?) -> Void) {
    
        let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
    
        let param: [String: Any] = [
            "access_token" : access_token,
            "status" : status
        ]
        let imageData = UIImageJPEGRepresentation(pic, 0.5)
        
        post(urlStr, parameters: param, constructingBodyWith: { (formData) in
            //拼接要上传的二进制图片
            formData.appendPart(withFileData: imageData!, name: "pic", fileName: "haha", mimeType: "image/jpeg")
        }, progress: nil, success: { (respose, _) in
            finishCallBack(respose, nil)
        }) { (_, error) in
            finishCallBack(nil, error)
        }
    
    
    }
    
}
